<?php
class CategoriesController extends AppController {

	var $name = 'Categories';
	var $helpers = array('Html', 'Form');

	var $uses = array('Category', 'Purchase', 'MonthTotal', 'YearTotal');

	var $paginate = array(
		'Category' => array(
			'order' => 'Category.name ASC'
		),
		'Purchase' => array(
			'limit' => 10
		)
	);

	function beforeFilter() {
		parent::beforeFilter();
		$this->set('menu_select', 'categories');
	}

	function index() {

		$month = $this->MonthTotal->find('all',
			array(
				'limit'=>5,
				'conditions' => array('date'=>date('Y-m-01', time())),
				'order'=>'MonthTotal.amount DESC'
			)
		);

		$year = $this->YearTotal->find('all',
			array(
				'limit'=>5,
				'conditions' => array('date'=>date('Y-01-01', time())),
				'order'=>'YearTotal.amount DESC'
			)
		);
//		pr($year);

		$tops = array();
		$tops[] = array('name'=>'month', 'date'=>date('Y/m'), 'title'=>'This Month ('.date('F').')', 'data'=>$month);
		$tops[] = array('name'=>'year', 'date'=>date('Y'), 'title'=>'This Year ('.date('Y').')', 'data'=>$year);

		$this->set('tops', $tops);

		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Category.', true));
			$this->redirect(array('action'=>'index'));
		}

		//TOTALS
		$tot = $this->Category->query("SELECT 'all' as period, SUM(amount) as amount FROM purchases WHERE category_id=$id");
		$year = date('Y', time()).'-01-01';
		$year_tot = $this->Category->query("SELECT 'year' as period, SUM(amount) as amount FROM purchases WHERE category_id=$id AND date >= '$year' ");
		$month = date('Y-m', time()).'-01';
		$month_tot = $this->Category->query("SELECT 'month' as period, SUM(amount) as amount FROM purchases WHERE category_id=$id AND date >= '$month' ");

		$tots = array_merge($tot, $year_tot, $month_tot);

		$tots = Set::extract($tots, '{n}.0');

		$tots = array_reverse($tots);

/*		$mt = $tots['month'];
		$tots['Month ('.date('m', time()).')'] = $mt;
		unset($tots['month']);
*/
		$this->set('totals', $tots);

//		pr($tots);

		$this->set('category', $this->Category->read(null, $id));
		$this->set('purchases', $this->paginate('Purchase', array('Purchase.category_id'=>$id)));
		$this->set('allCategories', $this->Category->find('list', array('conditions' => 'Category.id != '.$id, 'order'=>'Category.name ASC')));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Category->create();
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('The Category has been saved', true));
				$this->redirect(array('controller'=>'purchases', 'action'=>'qadd'));
			} else {
				$this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Category', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('The Category has been saved', true));
				$this->redirect(array('action'=>'view', $this->data['Category']['id']));
			} else {
				$this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Category->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Category', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Category->del($id)) {
			$this->Session->setFlash(__('Category deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function reassign() {

//		pr($this->data);

		$purchases = $this->data['Purchase']['id'];

		$cat_id = $this->data['Category']['id'];

		$this->Purchase->updateAll(
			array('Purchase.category_id' => $cat_id),
			array('Purchase.id' => $purchases)
		);

		$page = 1;
		$redirect = $this->data['Category']['redirect'];
		if ($redirect=='1') {
			$redirect = $cat_id;
		} else {
			$redirect = $this->data['Category']['original'];

			$page = $this->data['Category']['page'];
			if (!$page) {
				$page = 1;
			}
		}

		$this->redirect(array('controller' => 'categories', 'action'=>'view', $redirect, 'page'=>$page));
	}

	function super($name) {

		$this->set('super_name', $name);

		$categories = $this->Category->find('all',
			array(
				'conditions' => "Category.name LIKE '$name%' "
			)
		);

		$ids = Set::extract($categories, '{n}.Category.id');
		$purchases = $this->Purchase->find('all', array('conditions'=>array('Purchase.category_id'=>$ids)));

		$totals = $this->Category->getTotals(null, $ids);

		$this->set(compact('categories', 'purchases', 'totals'));

	}

}
?>